package com.cg.controller.map;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cg.controller.AbstractController;
import com.cg.entity.MapBuilding;
import com.cg.entity.MapData;
import com.cg.service.impl.MapBuildingService;
import com.cg.utils.Constant;
import com.cg.utils.Query;
import com.cg.utils.R;
import com.cg.utils.annotation.SysLog;
import com.google.gson.Gson;

@RestController
@RequestMapping("/sys/mapbuilding")
public class MapBuildingController extends AbstractController{
	@Autowired
	MapBuildingService mapBuildingService;
	
	Gson gson = new Gson();
	/**
	 * 所有列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("sys:mapbuilding:list")
	public R list(@RequestParam Map<String, Object> params){
		//只有超级管理员，才能查看所有管理员列表
		if(getUserId() != Constant.SUPER_ADMIN){
			params.put("createUserId", getUserId());
		}
		//查询列表数据
		Query query = new Query(params);
		List<MapBuilding> buidingList = mapBuildingService.queryList(query);
		int total = mapBuildingService.queryTotal(query);
		
		
		return R.success(total, "", buidingList);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("sys:mapbuilding:info")
	public R info(@PathVariable("id") Long id){
		MapBuilding building = mapBuildingService.queryObject(id);
		
		String str=MapData.replaceBlank(building.getMarker());
		MapData data = gson.fromJson(str, MapData.class);
		building.setMarker(String.valueOf(data.getData().get(0)));
		return R.ok().put("building", building);
	}
	
	/**
	 * 网格列表
	 */
	@RequestMapping("/select")
	@RequiresPermissions("sys:mapbuilding:select")
	public R select(String gridId){
		
		Map<String, Object> params = new HashMap<>();
		params.put("gridId", gridId);
		List<MapBuilding> list = mapBuildingService.queryList(params);
		
		return R.ok().put("list", list);
	}
	
	
	/**
	 * 保存用户
	 */
	@SysLog("保存")
	@RequestMapping("/save")
	@RequiresPermissions("sys:mapbuilding:save")
	public R save(@RequestBody MapBuilding building){
		
		MapData data = new MapData();
		List datalist =new ArrayList();
		datalist.add(building.getMarker());
		data.setData(datalist);
		String polygon = gson.toJson(data);
		
		building.setMarker(polygon);
		mapBuildingService.save(building);
		
		return R.ok();
	}
	/**
	 * 修改
	 */
	@SysLog("修改")
	@RequestMapping("/update")
	@RequiresPermissions("sys:mapbuilding:update")
	public R update(@RequestBody MapBuilding building){
		
//		comment.set(getUserId());
		MapData data = new MapData();
		List datalist =new ArrayList();
		datalist.add(building.getMarker());
		data.setData(datalist);
		String polygon = gson.toJson(data);
		
		building.setMarker(polygon);
		mapBuildingService.update(building);
		
		return R.ok();
	}
	
	/**
	 * 删除用户
	 */
	@SysLog("删除")
	@RequestMapping("/delete")
	@RequiresPermissions("sys:mapbuilding:delete")
	public R delete(@RequestBody Long[] ids){
		
		mapBuildingService.deleteBatch(ids);
		
		return R.ok();
	}
	
	
	
}
